from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.template.context_processors import csrf
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User, Group
from .models import case
from datetime import datetime
from home.context_processors import hasGroup
from appointments.models import Appointment
from django.contrib import messages
from stock.models import items
from bill.models import bill
from appointments.models import Appointment
# Create your views here.

# CREATE
@login_required
def generate(request):
    if hasGroup(request.user, 'doctor'):
        c = {}
        c.update(csrf(request))
        c['patients'] = User.objects.filter(groups__name='patient')
        c['appointments']=Appointment.objects.filter(appointment_done=0)
        return render(request, 'case/generate.html', c)
    messages.add_message(request, messages.WARNING, 'Access Denied.')
    return HttpResponseRedirect('/home')


@login_required
def doGenerate(request):
    if hasGroup(request.user, 'doctor'):
        appointment=Appointment.objects.get(pk=int(request.POST.get('appointment', '')))
        patient=User.objects.get(pk=appointment.patient_id)
        description = request.POST.get('description', '')
        filed_date = datetime.now()
        c = case(Appointment=appointment,patient=patient, receptionist=request.user,
                 description=description, filed_date=filed_date)
        c.save()

        item = items.objects.get(item_name='Consulting Charges')
        quantity = 1
        bill_create_date = datetime.now()
        bill_details = 'Basic Consulting Charges'
        ammount = item.sell_price*quantity
        b = bill(case=c, item=item, quantity=quantity,
                 bill_create_date=bill_create_date, bill_details=bill_details, ammount=ammount)
        b.save()

        appointment.appointment_done=1
        appointment.save()

        messages.add_message(request, messages.INFO,
                             'Successfully Generated Case')
        return HttpResponseRedirect('/home')
    messages.add_message(request, messages.WARNING, 'Access Denied.')
    return HttpResponseRedirect('/home')

# RETRIEVE
@login_required
def view(request):
    c = {}
    user = request.user
    cases = None
    if hasGroup(user, 'receptionist'):
        cases = case.objects.all()
    elif hasGroup(user, 'patient'):
        cases = case.objects.filter(patient=user)
    elif hasGroup(user, 'doctor'):
        c['isDoctor'] = True
        cases = [cases for cases in case.objects.filter(receptionist=user)]

    open = []
    closed = []
    for ca in cases:
        if ca.closed_date:
            closed.append(ca)
        else:
            open.append(ca)
    c['openCases'] = open
    c['closedCases'] = closed
    return render(request, 'case/view.html', c)

# UPDATE
@login_required
def close(request, id):
    user = request.user
    if hasGroup(user, 'doctor'):
        c = case.objects.get(id=id)
        c.closed_date = datetime.now()
        c.save()
        messages.add_message(request, messages.INFO,
                             'Successfully Closed Case')
        return HttpResponseRedirect('/case')
    messages.add_message(request, messages.WARNING, 'Access Denied.')
    return HttpResponseRedirect('/home')


# DELETE
@login_required
def delete(request, id):
    user = request.user
    if hasGroup(user, 'receptionist'):
        case.objects.get(id=id).delete()
        messages.add_message(request, messages.INFO,
                             'Successfully Closed Case')
        return HttpResponseRedirect('/case')
    messages.add_message(request, messages.WARNING, 'Access Denied.')
    return HttpResponseRedirect('/home')
